﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Ewk.Runtime.Serialization.UnitTests.Records.CharacterSeparated;
using Ewk.Runtime.Serialization.UnitTests.Records.PositionBased;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Ewk.Runtime.Serialization;
using Ewk.Runtime.Serialization.UnitTests.Records;
using Ewk.Runtime.Serialization.PositionBased;

namespace Ewk.Runtime.Serialization.UnitTests
{
    [TestClass]
    public class DatarecordSerializer_Int32Tests
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void DatarecordSerializer_can_serialize_an_instance_of_a_type_with_a_CharacterSeparatedDatarecordAttribute_with_an_integer_field()
        {
            var datarecord = new CharacterSeparatedTestDatarecordWithIntegerField { Field = 1 };

            var serializer = new DatarecordSerializer<CharacterSeparatedTestDatarecordWithIntegerField>();
            var result = serializer.Serialize(datarecord);
            
            Assert.AreEqual("1", result);
        }

        [TestMethod]
        public void DatarecordSerializer_can_deserialize_a_string_to_an_instance_of_a_type_with_a_CharacterSeparatedDatarecordAttribute_with_an_integer_field()
        {
            const string datarecord = "1";

            var serializer = new DatarecordSerializer<CharacterSeparatedTestDatarecordWithIntegerField>();
            var result = serializer.Deserialize(datarecord);

            Assert.AreEqual(1, result.Field);
        }

        [TestMethod, ExpectedException(typeof(DatafieldSerializationException))]
        public void DatarecordSerializer_cannot_deserialize_a_string_containing_an_invalid_value_to_an_instance_of_a_type_with_a_CharacterSeparatedDatarecordAttribute_with_an_integer_field()
        {
            const string serializedDatarecord = "x";

            var serializer = new DatarecordSerializer<CharacterSeparatedTestDatarecordWithIntegerField>();
            serializer.Deserialize(serializedDatarecord);
        }

        [TestMethod]
        public void DatarecordSerializer_can_deserialize_a_string_that_is_longer_than_the_record_definition_to_an_instance_of_a_type_with_a_PositionbasedDatarecordAttribute_with_an_integer_field_when_subscribed_to_the_DatarecordOverflow_event()
        {
            const string datarecord = "1234";

            string eventMessage = null;

            var serializer = new DatarecordSerializer<PositionBasedTestDatarecordWithIntegerField>();
            serializer.DatarecordOverflow += delegate(object sender, DatarecordOverflowEventArgs e)
                                                 {
                                                     eventMessage = e.Message;
                                                 };

            var result = serializer.Deserialize(datarecord);

            Assert.IsInstanceOfType(eventMessage, typeof(string));
            Assert.IsNotNull(result);
        }

        [TestMethod]
        public void DatarecordSerializer_can_deserialize_a_string_that_is_shorter_than_the_record_definition_to_an_instance_of_a_type_with_a_PositionbasedDatarecordAttribute_with_an_integer_field_when_subscribed_to_the_DatarecordIncomplete_event()
        {
            const string datarecord = "";

            string eventMessage = null;

            var serializer = new DatarecordSerializer<PositionBasedTestDatarecordWithIntegerField>();
            serializer.DatarecordIncomplete += delegate(object sender, DatarecordIncompleteEventArgs e)
                                                   {
                                                       eventMessage = e.Message;
                                                   };

            var result = serializer.Deserialize(datarecord);

            Assert.IsInstanceOfType(eventMessage, typeof(string));
            Assert.IsNotNull(result);
        }

        [TestMethod]
        public void DatarecordSerializer_can_deserialize_a_string_that_is_empty_to_an_instance_of_a_type_with_a_PositionbasedDatarecordAttribute_with_mandatory_fields_when_subscribed_to_the_DatarecordIncomplete_event()
        {
            var datarecord = new string(' ', 12);

            string eventMessage = null;

            var serializer = new DatarecordSerializer<PositionBasedTestDatarecordWithMandatoryFields>();
            serializer.DatafieldEmpty += delegate(object sender, DatafieldEmptyEventArgs e)
            {
                eventMessage = e.Message;
            };

            var result = serializer.Deserialize(datarecord);

            Assert.IsInstanceOfType(eventMessage, typeof(string));
            Assert.IsNotNull(result);
        }

        [TestMethod, ExpectedException(typeof(DatafieldEmptyException))]
        public void DatarecordSerializer_cannot_deserialize_a_string_that_is_empty_to_an_instance_of_a_type_with_a_PositionbasedDatarecordAttribute_with_mandatory_fields()
        {
            var datarecord = new string(' ', 12);

            var serializer = new DatarecordSerializer<PositionBasedTestDatarecordWithMandatoryFields>();
            serializer.Deserialize(datarecord);
        }

        [TestMethod, ExpectedException(typeof(DatarecordOverflowException))]
        public void DatarecordSerializer_cannot_deserialize_a_string_that_is_longer_than_the_record_definition_to_an_instance_of_a_type_with_a_PositionbasedDatarecordAttribute_with_an_integer_field_when_subscribed_to_the_DatarecordOverflow_event()
        {
            const string datarecord = "1234";

            var serializer = new DatarecordSerializer<PositionBasedTestDatarecordWithIntegerField>();
            serializer.Deserialize(datarecord);
        }

        [TestMethod, ExpectedException(typeof(DatarecordIncompleteException))]
        public void DatarecordSerializer_cannot_deserialize_a_string_that_is_shorter_than_the_record_definition_to_an_instance_of_a_type_with_a_PositionbasedDatarecordAttribute_with_an_integer_field_when_subscribed_to_the_DatarecordIncomplete_event()
        {
            const string datarecord = "";

            var serializer = new DatarecordSerializer<PositionBasedTestDatarecordWithIntegerField>();
            serializer.Deserialize(datarecord);
        }

        [TestMethod]
        public void DatarecordSerializer_serializes_an_instance_of_a_type_with_a_PositionBasedDatarecordAttribute_without_values_in_its_fields_to_a_string_with_the_default_character_of_the_PositionBasedDatarecordAttribute_if_no_default_characters_are_defined_on_the_fields()
        {
            var datarecord = new PositionBasedTestDatarecordWithIntegerField();

            var serializer = new DatarecordSerializer<PositionBasedTestDatarecordWithIntegerField>();
            var result = serializer.Serialize(datarecord);
            
            Assert.AreEqual(new string(' ', 1), result);
        }

        [TestMethod]
        public void DatarecordSerializer_deserializes_an_instance_of_a_string_with_the_default_character_of_the_PositionBasedDatarecordAttribute_to_a_type_with_a_PositionBasedDatarecordAttribute_without_values_in_its_fields_if_no_default_characters_are_defined_on_the_fields()
        {
            const string datarecord = " ";

            var serializer = new DatarecordSerializer<PositionBasedTestDatarecordWithIntegerField>();
            var deserializedRecord = serializer.Deserialize(datarecord);

            Assert.AreEqual(0, deserializedRecord.Field);
        }

        [TestMethod]
        public void DatarecordSerializer_can_serialize_an_instance_of_a_type_with_a_PositionBasedDatarecordAttribute_with_an_integer_field()
        {
            var datarecord = new PositionBasedTestDatarecordWithIntegerField { Field = 1 };

            var serializer = new DatarecordSerializer<PositionBasedTestDatarecordWithIntegerField>();
            var serializedRecord = serializer.Serialize(datarecord);

            Assert.AreEqual("1", serializedRecord);
        }

        [TestMethod]
        public void DatarecordSerializer_can_serialize_an_instance_of_a_type_with_a_PositionBasedDatarecordAttribute_with_an_integer_field_that_is_too_large_when_subscribed_on_the_DatafieldOverflow_event()
        {
            var datarecord = new PositionBasedTestDatarecordWithIntegerField { Field = 11 };

            string eventMessage = null;

            var serializer = new DatarecordSerializer<PositionBasedTestDatarecordWithIntegerField>();
            serializer.DatafieldOverflow += delegate(object sender, DatafieldOverflowEventArgs e)
                                                {
                                                    eventMessage = e.Message;
                                                };

            var serializedRecord = serializer.Serialize(datarecord);

            Assert.AreEqual("11", serializedRecord);
            Assert.IsInstanceOfType(eventMessage, typeof(string));
        }

        [TestMethod, ExpectedException(typeof(DatafieldOverflowException))]
        public void DatarecordSerializer_cannot_serialize_an_instance_of_a_type_with_a_PositionBasedDatarecordAttribute_with_an_integer_field_that_is_too_large_when_unsubscribed_on_the_DatafieldOverflow_event()
        {
            var datarecord = new PositionBasedTestDatarecordWithIntegerField { Field = 11 };

            var serializer = new DatarecordSerializer<PositionBasedTestDatarecordWithIntegerField>();
            serializer.DatafieldOverflow += SerializerDatafieldOverflow;
            serializer.DatafieldOverflow -= SerializerDatafieldOverflow;

            serializer.Serialize(datarecord);
        }

        [TestMethod]
        public void DatarecordSerializer_can_deserialize_a_string_to_an_instance_of_a_type_with_a_PositionBasedDatarecordAttribute_with_an_integer_field()
        {
            const string datarecord = "1";

            var serializer = new DatarecordSerializer<PositionBasedTestDatarecordWithIntegerField>();
            var deserializedRecord = serializer.Deserialize(datarecord);
            
            Assert.AreEqual(1, deserializedRecord.Field);
        }

        [TestMethod, ExpectedException(typeof(DatafieldSerializationException))]
        public void DatarecordSerializer_cannot_deserialize_a_string_containing_an_invalid_value_to_an_instance_of_a_type_with_a_PositionBasedDatarecordAttribute_with_an_integer_field()
        {
            const string serializedDatarecord = "x";

            var serializer = new DatarecordSerializer<PositionBasedTestDatarecordWithIntegerField>();
            serializer.Deserialize(serializedDatarecord);
        }

        static void SerializerDatafieldOverflow(object sender, DatafieldOverflowEventArgs e)
        {
        }
    }
}
